import { Service } from 'egg';
import {
  getUserByName,
  getUserById,
  registerCompany,
  register,
  getUserListByDept,
  dimission,
  create,
  save,
  createRootOrg,
} from '../sql/user';

export default class User extends Service {
  async getUserByName(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(getUserByName, params);
    ctx.logger.info('getUserByName:', sql, params);
    const res = await app.mysql.query(sql, param);
    const form = ctx.helper.toJsonObj(res[0]);
    return form;
  }

  async getUserById(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(getUserById, params);
    ctx.logger.info('getUserById:', sql, params);
    const res = await app.mysql.query(sql, param);
    const form = ctx.helper.toJsonObj(res[0]);
    return form;
  }

  async getUserListByDept(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(getUserListByDept, params);
    ctx.logger.info('getUserListByDept:', sql, param);
    let res: any = await app.mysql.query(sql, param);
    res = res.map((it: any) => {
      const item = ctx.helper.toJsonObj(it) || {};
      return item;
    });
    return res;
  }

  async register(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const formatParams = ctx.helper.toJsonString(params);
    // 先注册公司，再注册用户
    const { sql: sqlCompany, param: paramCompany } = ctx.helper.formatSql(
      registerCompany,
      formatParams,
    );
    ctx.logger.info('registerCompany:', sqlCompany, paramCompany);
    const resCompany: any = await app.mysql.query(sqlCompany, paramCompany);
    if (resCompany.affectedRows) {
      // 组织机构表生成公司顶级架构 , :
      Object.assign(formatParams, {
        orgId: params.companyId,
        orgName: params.companyName,
        role: 'systemManage',
        authStatus: 1,
        parentId: 'root',
        orgManage: params.userid,
      });
      {
        const { sql, param } = ctx.helper.formatSql(
          createRootOrg,
          formatParams,
        );
        ctx.logger.info('createRootOrg:', sql, param);
        app.mysql.query(sql, param);
      }
      const { sql, param } = ctx.helper.formatSql(register, formatParams);
      ctx.logger.info('register:', sql, param);
      const res = await app.mysql.query(sql, param);
      return res;
    }
  }

  async dimission(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const promises: any = [];
    params.userids.forEach((userid) => {
      const { sql, param } = ctx.helper.formatSql(dimission, { userid });
      promises.push(app.mysql.query(sql, param));
    });
    ctx.logger.info('dimission:', promises, params);
    return Promise.all(promises).then((res) => {
      return null;
    });
  }

  async create(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const userid = 'user_' + ctx.helper.getUuid();
    const password = ctx.helper.getUuid(6);
    Object.assign(params, { userid, password });
    const formatParams = ctx.helper.toJsonString(params);
    const { sql, param } = ctx.helper.formatSql(create, formatParams);
    ctx.logger.info('create:', sql, param);
    await app.mysql.query(sql, param);
    return null;
  }

  async save(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const formatParams = ctx.helper.toJsonString(params);
    const { sql, param } = ctx.helper.formatSql(save, formatParams);
    ctx.logger.info('save:', sql, param);
    await app.mysql.query(sql, param);
    return null;
  }

  async updateInfo(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const formatParams = ctx.helper.toJsonString(params);
    const field = ctx.helper.camel2snake(params.key);
    const sqlStr = `update user set
      ${field} = :value
      where userid = :userid
    `;
    const { sql, param } = ctx.helper.formatSql(sqlStr, formatParams);
    ctx.logger.info('register:', sql, param);
    const res = await app.mysql.query(sql, param);
    return res;
  }

  async updatePassword(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const formatParams = ctx.helper.toJsonString(params);
    const { sql, param } = ctx.helper.formatSql(register, formatParams);
    ctx.logger.info('register:', sql, param);
    const res = await app.mysql.query(sql, param);
    return res;
  }
}
